package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.chenfeng.vo.SearchIndexVo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.service.SearchService;
import org.apache.commons.lang3.RandomUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 搜索的Service实现类
 *
 * @author Chen_Feng
 * @since 2022/06/29 16:39
 */
@Service
@Transactional
public class SearchServiceImpl implements SearchService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    /**
     * 进入搜索页面时获取当前默认关键词、热搜和历史搜索
     *
     * @return com.cskaoyan.bean.chenfeng.vo.SearchIndexVo
     * @author Chen_Feng
     * @since 2022/06/29 16:45
     */
    @Override
    public SearchIndexVo getSearchIndex() {

        // 创建返回对象
        SearchIndexVo searchIndexVo = new SearchIndexVo();
        List<SearchIndexVo.SearchHistoryVo> historyKeywordList = Arrays.asList();

        // 获取默认搜索，从列表中随机获得一个默认的搜索词
        List<MarketKeyword> defaultList = marketKeywordMapper.selectDefaultKeyword();
        MarketKeyword defaultKeyword = getOneKeywordByRandom(defaultList);

        // 获取所有热搜词
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.createCriteria().andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<MarketKeyword> hotKeywordList = marketKeywordMapper.selectByExample(marketKeywordExample);

        // 获取历史搜索
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            Integer userId = user.getId();
            historyKeywordList = marketSearchHistoryMapper.selectByUserId(userId);
        }

        // 封装数据
        searchIndexVo.setHistoryKeywordList(historyKeywordList);
        searchIndexVo.setDefaultKeyword(defaultKeyword);
        searchIndexVo.setHotKeywordList(hotKeywordList);

        return searchIndexVo;
    }

    /**
     * 删除（隐藏）历史搜索的关键词
     *
     * @return com.cskaoyan.bean.chenfeng.vo.SearchIndexVo
     * @author Chen_Feng
     * @since 2022/06/29 17:48
     */
    @Override
    public boolean clearHistory() {

        // 获取用户id
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            Integer userId = user.getId();
            Integer affectedRows = marketSearchHistoryMapper.updateDeletedToTrueByUserId(userId);
            return affectedRows != null && affectedRows > 0;
        }
        return true;
    }

    /**
     * 根据用户的输入即时查询关键词表
     *
     * @param keyword 用户输入的关键词
     * @return java.util.List<java.lang.String>
     * @author Chen_Feng
     * @since 2022/06/29 18:13
     */
    @Override
    public List<String> getHelper(String keyword) {

        // 模糊查询
        List<String> list = marketKeywordMapper.selectKeywordFuzzy("%" + keyword + "%");

        return list;
    }

    /**
     * 随机从关键词集合中取出一个
     *
     * @param list 集合
     * @return com.cskaoyan.bean.MarketKeyword
     * @author Chen_Feng
     * @since 2022/06/29 17:23
     */
    private MarketKeyword getOneKeywordByRandom(List<MarketKeyword> list) {
        int i = RandomUtils.nextInt(0, list.size());
        return list.get(i);
    }
}
